package com.ciwong.epaper.modules.epaper.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.ciwong.epaper.R;
import com.ciwong.epaper.modules.epaper.bean.Module;
import com.ciwong.epaper.modules.epaper.bean.ModuleInfo;

import java.util.List;

/**
 *
 * 内容显示页面的画廊的adapter Created by res-jianpingliu on 2015/8/6.
 */
public class ContentGalleryAdapter extends BaseAdapter
{
    private List<Module> moduleList;

    private LayoutInflater inflater;

    private int width;

    public ContentGalleryAdapter(Context context, List<Module> modules)
    {
        moduleList = modules;
        inflater = LayoutInflater.from(context);
        width = context.getResources().getDisplayMetrics().widthPixels;
    }

    @Override
    public int getCount()
    {
        return moduleList == null ? 0 : moduleList.size();
    }

    @Override
    public Object getItem(int position)
    {
        return moduleList == null ? 0 : moduleList.get(position);
    }

    @Override
    public long getItemId(int position)
    {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent)
    {
        IndexItem item = null;
        if (convertView == null)
        {
            item = new IndexItem();
            convertView = inflater.inflate(R.layout.adapter_content_index_item,
                    null);
            item.iconIv = (ImageView) convertView
                    .findViewById(R.id.item_content_index_icon_iv);
            item.nameTv = (TextView) convertView
                    .findViewById(R.id.item_content_index_name_tv);
            convertView.setTag(item);
        }
        else
        {
            item = (IndexItem) convertView.getTag();
        }

        RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) item.nameTv.getLayoutParams();
        params.width = width / 3;
        item.nameTv.setLayoutParams(params);

        ModuleInfo moduleInfo = moduleList.get(position).getModuleInfo();
        item.nameTv.setText(moduleInfo.getModuleName());
        int drawableId = 0;
        switch (moduleInfo.getModuleId())
        {
        // 同步测试
            case ModuleInfo.ModuleInfoId.MODULE_ID_TEST:
                drawableId = R.mipmap.content_icon_dycs;
                break;
            // 时文
            case ModuleInfo.ModuleInfoId.MODULE_ID_ESSAY:
                drawableId = R.mipmap.content_icon_sw;
                break;
            // 同步训练
            case ModuleInfo.ModuleInfoId.MODULE_ID_TRAIN:
                drawableId = R.mipmap.content_icon_tbjl;
                break;
            // 同步跟读
            case ModuleInfo.ModuleInfoId.MODULE_ID_REPEAT:
                drawableId = R.mipmap.content_icon_tbgd;
                break;
            // 听说模考
            case ModuleInfo.ModuleInfoId.MODULE_ID_LISTEN_SPEAK_EXAM:
                drawableId = R.mipmap.content_icon_tsmk;
                break;
            // 微技能训练
            case ModuleInfo.ModuleInfoId.MODULE_ID_MICRO_SKILLS:
                drawableId = R.mipmap.content_icon_jnxl;
                break;
            // 报听写
            case ModuleInfo.ModuleInfoId.MODULE_ID_DICTATION:
                drawableId = R.mipmap.content_icon_btl;
                break;

            // 视频讲解
            case ModuleInfo.ModuleInfoId.MODULE_ID_VIDEO_EXPLAIN:
                drawableId = R.mipmap.icon_video_explain;
                break;
            // 在线作答
            case ModuleInfo.ModuleInfoId.MODULE_ID_ONLINE_ANSWER:
                drawableId = R.mipmap.icon_online_answer;
                break;
        }


        item.iconIv.setBackgroundResource(drawableId);
        return convertView;
    }

    private class IndexItem
    {
        ImageView iconIv;

        TextView nameTv;
    }
}
